package com.alex.j2se.stream;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

/**
 * Author  : gaojun            <br/>
 * Date    : 2020/3/4                          <br/>
 * Time    : 12:14 下午                         <br/>
 * ---------------------------------------    <br/>
 * Desc    :
 */
public class MapTest {

    @Test
    public void groupToAttributeMap() {
        List<Person> personList = Lists.newLinkedList();
        personList.add(new Person(1,"a1"));
        personList.add(new Person(2,"a2"));
        personList.add(new Person(1,"a3"));
        personList.add(new Person(2,null));

        Map map = personList.stream().collect(Collectors.toMap(p -> p.getId(), p -> p.getName(), (n1, n2) -> n2));
        System.out.println(map);
    }

    @Test
    public void groupToAttributeObjListMap() {
        List<Person> personList = Lists.newLinkedList();
        personList.add(new Person(1,"a1"));
        personList.add(new Person(2,"a2"));

        System.out.println(personList.stream().collect(Collectors.groupingBy(Person::getId)).keySet());
        System.out.println(personList.stream().collect(Collectors.groupingBy(Person::getId)).get(1).size());
    }

    @Test
    public void groupToAttributeObjMap() {
        List<Person> personList = Lists.newLinkedList();
        personList.add(new Person(1,"a1"));
        personList.add(new Person(2,"a2"));

        System.out.println(personList.stream().collect(Collectors.toMap(p -> p.getId(), p -> p)));
    }

    @Test
    public void mapToList() {
        List<Person> personList = Lists.newLinkedList();
        personList.add(new Person(1,"a1"));
        personList.add(new Person(2,"a2"));
        System.out.println(personList.stream().map(p -> p.getName()).collect(Collectors.toList()));
    }

    @Test
    public void computeTest() {
        HashMap<String, Integer> repeatTimesMap = new HashMap<>();
        repeatTimesMap.put("key1", 1);
        repeatTimesMap.put("key2", 2);
        repeatTimesMap.put("key1_1", 3);
        repeatTimesMap.put("key2_1", 4);
        System.out.println(repeatTimesMap);
        BiFunction<String, Integer, Integer> stringIntegerIntegerBiFunction = (k, v) -> (k.contains("1")) ? ++v : v;
        repeatTimesMap.compute("key1", stringIntegerIntegerBiFunction);
        repeatTimesMap.compute("key2", stringIntegerIntegerBiFunction);
//        repeatTimesMap.compute("key3", stringIntegerIntegerBiFunction); npe
        System.out.println(repeatTimesMap);
    }

    @Test
    public void mergeTest() {
        HashMap<String, Integer> repeatTimesMap = new HashMap<>();
        repeatTimesMap.put("key1", 1);
        repeatTimesMap.put("key2", 2);
        repeatTimesMap.put("key1_1", 3);
        repeatTimesMap.put("key2_1", 4);
        System.out.println(repeatTimesMap);
        repeatTimesMap.merge("key1", 2, (k,v) -> v + 1);
        System.out.println(repeatTimesMap);
    }

    @Test
    public void computeIfPrentTest() {
//        List<Student> personList = Lists.newLinkedList();
//        personList.add(new Student("a1","football", 5));
//        personList.add(new Student("a2","basketball", 8));
//        personList.add(new Student("a3","football", 5));
//        Map<String,Integer> hobbyTotalAge = Maps.newHashMap();
//        personList.forEach(student -> {
////            if (!hobbyTotalAge.containsKey(student.getHobby())) {
////                hobbyTotalAge.putIfAbsent(student.getHobby(), student.getAge());
////            }else {
//                hobbyTotalAge.computeIfPresent(student.getHobby(), (k, v) -> v + student.getAge());
////            }
//        });
//        System.out.println(hobbyTotalAge);
        System.out.println(false&&false||true);
    }
}
